﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using HalalCheck.CustomViews;
using HalalCheck.Resources;
using SpotOffer.Resource;
using Xamarin.Forms;
using XLabs.Forms.Controls;

namespace HalalCheck.View
{
    public class HalalItemPage : BasePage
    {

        public HalalItemPage()
        {
            SetBinding(TitleProperty, new Binding("SelectedHalalItem.Name"));
        }

        protected override void OnAppearing()
        {
            base.OnAppearing();
            var layout = new RelativeLayout
            {
                VerticalOptions = LayoutOptions.FillAndExpand,
                HorizontalOptions = LayoutOptions.FillAndExpand,
            };
            //  layout.Children.Add(new TileViewHalal(), Constraint.Constant(0), Constraint.Constant(0), Constraint.RelativeToParent((p => p.Bounds.Width)), Constraint.RelativeToParent((p) => p.Bounds.Height));

            var halalchecklogo = new Image
            {
                Source = "centerlogo.png"
            };
            layout.Children.Add(halalchecklogo, Constraint.RelativeToParent((p) => (p.Bounds.Width - 200) / 2), Constraint.RelativeToParent((p) => (p.Bounds.Height - 200) / 2), Constraint.RelativeToParent((p => 200)), Constraint.RelativeToParent((p) => 200));

            var halalItemLayout = new RelativeLayout();
            var background = new RoundedBox
            {
                BackgroundColor = AppColors.White,
                Opacity = 0.95,
                CornerRadius = 10
            };
            halalItemLayout.Children.Add(background, Constraint.Constant(0), Constraint.Constant(0),
                Constraint.RelativeToParent((p) => p.Bounds.Width), Constraint.RelativeToParent((p) => p.Bounds.Height));

            layout.Children.Add(halalItemLayout, Constraint.Constant(AppMargins.ItemPadding), Constraint.Constant(AppMargins.ItemPadding), Constraint.RelativeToParent((p => p.Bounds.Width - AppMargins.ItemPadding * 2)), Constraint.RelativeToParent((p) => p.Bounds.Height - AppMargins.ItemPadding * 2));

            var halalLabelBackground = AppViews.WhiteRounded();
            halalLabelBackground.SetBinding(Label.BackgroundColorProperty, new Binding("SelectedHalalItem.HalalColor"));
            halalItemLayout.Children.Add(halalLabelBackground, Constraint.Constant(AppMargins.ItemMargin), Constraint.Constant(AppMargins.ItemMargin), Constraint.RelativeToParent((p) => p.Bounds.Width - AppMargins.ItemMargin * 2), Constraint.Constant(40));

            var halalLabel = new ExtendedLabel
            {
                TextColor = Color.White,
                YAlign = TextAlignment.Center,
                XAlign = TextAlignment.Center,
                FontAttributes = FontAttributes.Bold,
                FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
                BackgroundColor = Color.Transparent,
                //FontName = "Harabara.otf",          
            };
            halalLabel.SetBinding(Label.TextProperty, new Binding("SelectedHalalItem.HalalText"));
            halalItemLayout.Children.Add(halalLabel, Constraint.Constant(AppMargins.ItemMargin), Constraint.Constant(AppMargins.ItemMargin), Constraint.RelativeToParent((p) => p.Bounds.Width - AppMargins.ItemMargin * 2), Constraint.Constant(40));


            var titleLabel = new ExtendedLabel
            {
                TextColor = AppColors.OrigDarkBlue,
                Text = "titleLabel",
               // FontAttributes = FontAttributes.Bold,
                FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
                //FontName = "Harabara.otf",
                
            };
            titleLabel.SetBinding(Label.TextProperty, new Binding("SelectedHalalItem.Id"));
            halalItemLayout.Children.Add(titleLabel, Constraint.Constant(AppMargins.ItemMargin), Constraint.RelativeToView(halalLabel, (p, s) => s.Bounds.Bottom), Constraint.RelativeToParent((p) => p.Bounds.Width - AppMargins.ItemMargin * 2));

            var nameLabel = new ExtendedLabel
            {
                TextColor = AppColors.OrigDarkBlue,
                Text = "nameLabel",
                FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
                //FontName = "Harabara.otf",
                
            };
            nameLabel.SetBinding(Label.TextProperty, new Binding("SelectedHalalItem.Name"));
            halalItemLayout.Children.Add(nameLabel, Constraint.Constant(AppMargins.ItemMargin), Constraint.RelativeToView(titleLabel, (p, s) => s.Bounds.Bottom), Constraint.RelativeToParent((p) => p.Bounds.Width - AppMargins.ItemMargin * 2));

            var descriptionLabel = new ExtendedLabel
            {
                TextColor = AppColors.OrigDarkBlue,
                Text = "descriptionLabel",
                FontSize = Device.GetNamedSize(NamedSize.Large, typeof(Label)),
                //FontName = "Harabara.otf",
                
                // LineBreakMode = LineBreakMode.WordWrap
            };
            descriptionLabel.SetBinding(Label.TextProperty, new Binding("SelectedHalalItem.Description"));
            halalItemLayout.Children.Add(descriptionLabel, Constraint.Constant(AppMargins.ItemMargin), Constraint.RelativeToView(nameLabel, (p, s) => s.Bounds.Bottom), Constraint.RelativeToParent((p) => p.Bounds.Width - AppMargins.ItemMargin * 2));

           // halalItemLayout.LayoutTo(new Rectangle(AppMargins.ItemPadding, AppMargins.ItemPadding, Width - 2 * AppMargins.ItemPadding, Height - 2 * AppMargins.ItemPadding), 500, Easing.Linear);

            Content = layout;
        }
    }
}
